package handler

import (
	"context"
	"emailcenter/internal/cache"
	"emailcenter/internal/mail"
	"emailcenter/internal/service"
	"emailcenter/utils/resx"
)

type EmailService struct {
	service.UnimplementedEmailServiceServer
}

func (e *EmailService) EmailGenerateLink(ctx context.Context, req *service.EmailLinkGenerateRequest) (*service.EmailLinkGenerateResponse, error) {
	err := mail.SendMailWithUUID(
		req.Email,
		req.Recipient,
		req.Uuid,
	)
	if err != nil {
		return &service.EmailLinkGenerateResponse{Code: resx.SystemErr}, err
	}
	return &service.EmailLinkGenerateResponse{Code: resx.Success}, nil
}

func (e *EmailService) EmailVerifyCaptcha(ctx context.Context, req *service.EmailCaptchaVerifyRequest) (*service.EmailCaptchaVerifyResponse, error) {
	res, err := cache.RedisDB.Get("email:" + req.Operate + ":" + req.Email).Result()
	if res == req.Captcha && err == nil {
		return &service.EmailCaptchaVerifyResponse{Code: resx.Success}, nil
	} else {
		return &service.EmailCaptchaVerifyResponse{Code: resx.Fail}, nil
	}
}

func (e *EmailService) EmailRemind(ctx context.Context, req *service.EmailRemindRequest) (*service.EmailRemindResponse, error) {
	err := mail.SendRemindMail(req.Email, req.Content)
	if err != nil {
		return &service.EmailRemindResponse{Code: resx.SystemErr}, nil
	}
	return &service.EmailRemindResponse{Code: resx.Success}, nil
}
